﻿using SkladLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ContentViewLib
{
    /// <summary>
    /// Interaction logic for userContentView.xaml
    /// </summary>
    public partial class userContentView : UserControl
    {
        public delegate void ChangetItemsHandler(object sender);
        public event ChangetItemsHandler ChangedItems;


        string table = "Nevracene";
        public userContentView()
        {
            InitializeComponent();
            listView.DataContextChanged += listView_DataContextChanged;
        }

        void listView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }

        void timRefresher_Tick(object sender, EventArgs e)
        {
            RefreshContent();
        }

        public void RefreshContent()
        {
            Graphic.RowsConvertor.outDatedCount = 0;
            DataLib.Settings.databases.dbPujcky.loadAllSortTo("id", table, listView);
        }

        public void RefreshContent(string table)
        {
            DataLib.Settings.databases.dbPujcky.loadAllSortTo("id", table, listView);
        }

        public void Search(Dictionary<string, string> values, string table)
        {
            if (table != this.table)
            {
                this.table = table;
                RefreshContent();
            }
            if (values.Count > 0)
            {
                DataLib.Settings.databases.dbPujcky.SearechSortAndShow(table, values, listView);

            }
            else
            {
                RefreshContent();
            }
        }

        public ListView GetListView()
        {
            return listView;
        }

        private int GetSelectedID()
        {
            try
            {
                System.Data.DataRowView value = (System.Data.DataRowView)listView.SelectedValue;
                if (listView.SelectedIndex != -1)
                {
                    value = (System.Data.DataRowView)listView.SelectedValue;
                }
                return int.Parse(value["id"].ToString());
            }
            catch
            {
                return -1;
            }
        }

        private string GetSelectedZboziID()
        {
            try
            {
                System.Data.DataRowView value = (System.Data.DataRowView)listView.SelectedValue;
                if (listView.SelectedIndex != -1)
                {
                    value = (System.Data.DataRowView)listView.SelectedValue;
                }
                return value["zboziID"].ToString();
            }
            catch
            {
                return "";
            }
        }

        public void zvyraznit()
        {
            try
            {
                System.Data.DataRowView value = (System.Data.DataRowView)listView.SelectedValue;
                if (listView.SelectedIndex != -1)
                {
                    value = (System.Data.DataRowView)listView.SelectedValue;
                }
                Global.hightlRow = value["idZakaznika"].ToString();
            }
            catch
            {
                Global.hightlRow = "";
            }
        }

        private void listView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            zvyraznit();
        }

        private void cmiSmazat_Click_1(object sender, RoutedEventArgs e)
        {
            if (Zbozi.ExistZboziByCode(GetSelectedZboziID()))
                Zbozi.GetZboziByCode(GetSelectedZboziID()).SetPujceno(false);

            int selectedId = GetSelectedID();
            if (selectedId != -1)
                DataLib.Settings.databases.dbPujcky.DeleteRow(table, "id", selectedId.ToString());
            RefreshContent();
            if (ChangedItems != null)
                ChangedItems(this);
        }

        private void cmiVraceno_Click_1(object sender, RoutedEventArgs e)
        {
            Vraceno();
        }

        private void Vraceno()
        {
            if (listView.Items.Count > 0 && table == "Nevracene")
            {
                if (Zbozi.ExistZboziByCode(GetSelectedZboziID()))
                    Zbozi.GetZboziByCode(GetSelectedZboziID()).SetPujceno(false);

                if (!DataLib.Settings.databases.dbPujcky.ExistTable("Vracene" + DateTime.Now.Year))           //Overeni existence tabulky pro novy rok
                    DataLib.Settings.databases.dbPujcky.CloneTable("Nevracene", "Vracene" + DateTime.Now.Year);         //Vytvoreni tabulku podle te puvodni

                Dictionary<string, string> row = DataLib.Settings.databases.dbPujcky.GetRow("Nevracene", GetSelectedID().ToString());
                DataLib.Settings.databases.dbPujcky.AddRow("Vracene" + DateTime.Now.Year, row);            //Nahrani radku do tabulky vracenych
                DataLib.Settings.databases.dbPujcky.DeleteRow("Nevracene", "id", GetSelectedID().ToString());   //Smazani radku z nevracene
                if (ChangedItems != null)
                    ChangedItems(this);
            }
            else
            {
                if (Zbozi.ExistZboziByCode(GetSelectedZboziID()))
                    Zbozi.GetZboziByCode(GetSelectedZboziID()).SetPujceno(false);

                if (!DataLib.Settings.databases.dbPujcky.ExistTable("Nevracene" ))           //Overeni existence tabulky pro novy rok
                    DataLib.Settings.databases.dbPujcky.CloneTable(table, "Nevracene");         //Vytvoreni tabulku podle te puvodni

                Dictionary<string, string> row = DataLib.Settings.databases.dbPujcky.GetRow(table, GetSelectedID().ToString());
                row.Remove("id");
                DataLib.Settings.databases.dbPujcky.AddRow("Nevracene", row);            //Nahrani radku do tabulky vracenych
                DataLib.Settings.databases.dbPujcky.DeleteRow(table, "id", GetSelectedID().ToString());   //Smazani radku z nevracene
                if (ChangedItems != null)
                    ChangedItems(this);
            }
        }

        private void cmiEditovat_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void listView_MouseEnter(object sender, MouseEventArgs e)
        {
            if (listView.Items.Count < 1)
            {
                ctMenu.Visibility = Visibility.Hidden;

            }
            else
            {
                ctMenu.Visibility = Visibility.Visible;
            }
        }

        private void listView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Vraceno();
        }

        public int GetCountExpiredItems()
        {
            return DataLib.Settings.databases.dbPujcky.QueryGetInt("SELECT COUNT(*) FROM Nevracene WHERE (do < CONVERT(DateTime, '" + DateTime.Now.Date.ToString("yyyy-MM-dd HH:mm:ss") + "',120))");
        }


    }
}
